# = test.user.1.rb - Change Password Test
# See LICENSE.txt for licensing details.
#
# == Description
#To test the functionality to change password fo the user 
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
    
	class TC_ChangePassword_test < Test::Unit::TestCase
   
    def startup
      $NewPassword = 'newpassword'      
      @@ie = Watir::IE.start($SITE_URL)
	  end
	
	  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
	  end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))	
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_navigateToChangePasswordPage
      ie.goto($SITE_URL + 'user/ChangePassword.aspx')      
    end
    
    def test_07_verifyLinksOnChangePasswordPage
      assert(ie.link(:text, 'admin').exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
      assert(ie.link(:text, 'Control Panel').exists?)
    end
    
    def test_08_verifyFieldsOnChangePasswordPage
      assert(ie.button(:id, /ChangePasswordButton$/).exists?)
      assert(ie.text_field(:id, /TitleBarSearchText$/).exists?)
      assert(ie.text_field(:id, /Password$/).exists?)
      assert(ie.text_field(:id, /NewPassword1$/).exists?)
      assert(ie.text_field(:id, /NewPassword2$/).exists?)
    end
    
    def test_09_populateFieldsAndChangePassword
      ie.text_field(:id, /Password$/).value = $ADMIN_PASSWORD
      ie.text_field(:id, /NewPassword1$/).value = $NewPassword
      ie.text_field(:id, /NewPassword2$/).value = $NewPassword
      ie.button(:id, /ChangePasswordButton$/).click
    end
    
    def test_10_logoutAndTryNewPassword
      ie.goto $SITE_URL 
      test_02_verifyLoginLinksExist
      
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
      
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $NewPassword
      assert(ie.text_field(:id, /password$/).verify_contains($NewPassword))
      
      test_05_submitLoginForm
      assert(ie.link(:text, 'admin').exists?)
    end

    def test_11_revertPassword
      test_06_navigateToChangePasswordPage
      test_07_verifyLinksOnChangePasswordPage
      test_08_verifyFieldsOnChangePasswordPage
      
      ie.text_field(:id, /Password$/).value = $NewPassword
      ie.text_field(:id, /NewPassword1$/).value = $ADMIN_PASSWORD
      ie.text_field(:id, /NewPassword2$/).value = $ADMIN_PASSWORD
      ie.button(:id, /ChangePasswordButton$/).click
    end
    
    def test_12_logoutAndTryOldPassword
      ie.goto $SITE_URL
      test_02_verifyLoginLinksExist
      test_03_navigateToLogin
      
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
      
      test_05_submitLoginForm
      assert(ie.link(:text, 'admin').exists?)
    end
    
  end